﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xrm.Sdk;



namespace ScriptEase
{
    /// <summary>
    /// CRM Organization related functionality.
    /// </summary>
    internal class CrmOrganization
    {
        private readonly IOrganizationService _orgService;



        public CrmOrganization(IOrganizationService orgService)
        {
            _orgService = orgService;
        }



        /// <summary>
        /// Gets the all visible solutions of the connected organization.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Solution> GetSolutions()
        {
            using (ServiceContext ctx = new ServiceContext(_orgService))
            {
                return ctx.SolutionSet.Where(so => so.IsVisible.Value).ToList();
            }
        }



        /// <summary>
        /// Gets the solution web resource components.
        /// </summary>
        /// <param name="solutionId">The solution id.</param>
        /// <returns></returns>
        public List<SolutionComponent> GetSolutionWebResourceComponents(Guid solutionId)
        {
            using (ServiceContext ctx = new ServiceContext(_orgService))
            {
                //sc.ComponentType.Value, sc.ObjectId 
                return ctx.SolutionComponentSet.Where(sc => sc.SolutionId != null && sc.SolutionId.Id == solutionId 
                                                   && sc.ComponentType.Value == Convert.ToInt32( componenttype.WebResource)).ToList();
            }
        }



        /// <summary>
        /// Get all customizable, managed and unmanaged WebResources from CRM.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<WebResource> GetWebResources(bool includeManaged, bool includeNonCustomizable)
        {
            using (ServiceContext ctx = new ServiceContext(_orgService))
            {
                var webres = ctx.WebResourceSet.Where(wr =>
                                                      (
                                                          wr.WebResourceType.Value == WebResourceType.Script ||
                                                          wr.WebResourceType.Value == WebResourceType.StylesheetXsl ||
                                                          wr.WebResourceType.Value == WebResourceType.StyleSheetCss ||
                                                          wr.WebResourceType.Value == WebResourceType.Data ||
                                                          wr.WebResourceType.Value == WebResourceType.Webpage
                                                      ) && !wr.IsHidden.Value).ToList();

                
                return
                    webres.Where(
                        wr => (includeManaged || !wr.IsManaged.GetValueOrDefault()) && (includeNonCustomizable || wr.IsCustomizable.Value));
            }
        }



        /// <summary>
        /// Gets the specified,customizable and unmanaged WebResource from CRM.
        /// </summary>
        /// <param name="webResourceName">Name of the web resource.</param>
        /// <returns>WebResource or <c>null</c></returns>
        public WebResource GetWebResource(string webResourceName)
        {
            using (ServiceContext ctx = new ServiceContext(_orgService))
            {
                return ctx.WebResourceSet.FirstOrDefault(wr => wr.IsCustomizable.Value && wr.Name == webResourceName);
            }
        }
    }
}